package com.v2future.sjt.collegecheckingattendance.common.activity;

import android.content.Intent;
import android.view.View;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.leave.CourseLeaveActivity;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.DateUtil;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;

import java.util.ArrayList;
import java.util.List;

import cn.aigestudio.datepicker.bizs.calendars.DPCManager;
import cn.aigestudio.datepicker.cons.DPMode;
import cn.aigestudio.datepicker.views.DatePicker;

/**
 * Created by Administrator on 2016/1/25.
 * 课时假选择日期，外出假选择日期
 */
public class DatePickerActivity extends MainActivity{

    DatePicker dp;

    private List<String> selectDates;
    private String selectDate;

    private boolean isOutLeave;//是否是外出假

    /**
     * 标识：选中日期
     * 格式2016-2-11
     */
    public static final String TAG_SELECT_DATE = "selectDate";

    /**
     * 标识：请假类型
     */
    public static final String TAG_LEAVE_TYPE = "leave_type";

    /**
     * 请假类型
     */
    public static final int LEAVE_TYPE_COURSE = 1;//课时假
    public static final int LEAVE_TYPE_OUT = 2;//外出假
    public static final int ROLLCALL = 3;//点名选择日期
    private static int leaveType = LEAVE_TYPE_COURSE;



    @Override
    public void init() {
        leaveType = getIntent().getIntExtra(TAG_LEAVE_TYPE, LEAVE_TYPE_COURSE);
        isOutLeave = getIntent().getBooleanExtra(IntentKey.LEAVE_OUT, false);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_date_selector);
        setTitleText("日期");
        initTitleBack(null);
        dp = (DatePicker) findViewById(R.id.main_dp);
    }

    @Override
    public void initAfter() {
        int month = Integer.parseInt(DateUtil.getCurrentDateMonth());//获取当前月份
        dp.setDate(2016, month);
        dp.setTodayDisplay(true);
        if (leaveType == LEAVE_TYPE_COURSE) {
            dp.setMode(DPMode.SINGLE);
            dp.setOnDatePickedListener(courseLeaveListener);
        }
        else if (leaveType == LEAVE_TYPE_OUT) {
            initOutLeave();
            dp.setMode(DPMode.MULTIPLE);
            dp.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
                @Override
                public void onDateSelected(List<String> date) {
                    selectDates = date;
                }
            });
        }
        else if(leaveType == ROLLCALL){
            dp.setMode(DPMode.SINGLE);
            dp.setOnDatePickedListener(rollcallListener);
        }
    }

    /**
     * 初始化外出假
     */
    private void initOutLeave() {
        setRightText(getString(R.string.comfirm), outLeaveListener);
    }

    /**
     * 课时假事件
     */
    DatePicker.OnDatePickedListener courseLeaveListener = new DatePicker.OnDatePickedListener() {
        @Override
        public void onDatePicked(String date) {
            Intent backIntent = new Intent(DatePickerActivity.this, CourseLeaveActivity.class);
            backIntent.setAction(CourseLeaveActivity.BACKTAG_DATE_SELECT);
            backIntent.putExtra(TAG_SELECT_DATE, date);
            gobackWithData(backIntent);
        }
    };

    /**
     * 点名选择时间
     */
    DatePicker.OnDatePickedListener rollcallListener = new DatePicker.OnDatePickedListener() {
        @Override
        public void onDatePicked(String date) {
            Intent backIntent = new Intent(DatePickerActivity.this, HomeActivity.class);
            backIntent.setAction(CourseLeaveActivity.BACKTAG_DATE_SELECT);
            backIntent.putExtra(TAG_SELECT_DATE, date);
            backIntent.putExtra("id", HomeActivity.FLAG_ROLLCALL);
            gobackWithData(backIntent);
        }
    };

    /**
     * 外出假事件-单日期
     */
    View.OnClickListener outLeaveListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showTwoBtnDialog(getString(R.string.notice), "确定要申请外出假吗？", getString(R.string.cancel), null, getString(R.string.comfirm), submitLeaveListener);
        }

    };

    /**
     * 请假事件
     */
    View.OnClickListener submitLeaveListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            HideDialog();
            cancelLoadingDialog();
            showLoadingDialog();
            doSubmitLeave();
        }
    };

    private void doSubmitLeave(){
        BaseRequestParams params = new BaseRequestParams(this, formatUrlDo(Constants.PUBLISH_EVENT));
        params.addQueryStringParameter("type", "2");//请假状态写死
        params.addQueryStringParameter("leaveType", "3");//外出假为3
        params.addQueryStringParameter("leaveReason", "");
        params.addQueryStringParameter("weekDay", "");
        params.addQueryStringParameter("termId", "1");
        params.addQueryStringParameter("leaveDate", selectDate);//请假日期
        NetUtils.doGetRequest(this, params, new NetTaskListener<BaseResponseBean>() {

            @Override
            public void onSuccess(BaseResponseBean result) {
                tip("提交请假完成");
                finish();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(BaseResponseBean result) {
                tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                cancelLoadingDialog();
            }

            @Override
            public void onSuccessCache(BaseResponseBean result) {

            }
        });
    }
}
